package com.hskj.tool.app_tool;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;

import java.io.File;

import io.flutter.Log;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;

/**
 * AppToolPlugin
 */
public class AppToolPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware, PluginRegistry.ActivityResultListener {

    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private MethodChannel channel;
    private Context mContext;
    private Activity mActivity;
    private Result result;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "app_tool");
        channel.setMethodCallHandler(this);
        this.mContext = flutterPluginBinding.getApplicationContext();
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        this.result = result;
        if (call.method.equals("install_apk")) {
            //apk路径
            String path = call.argument("path");
            //apk 名
            String name = call.argument("applicationName");
            //安裝
            result.success(installApk(path) ? "1" : "0");
        } else if (call.method.equals("skip")) {
            //应用跳转
            String skip = call.argument("skip");
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_BROWSABLE);
                intent.setComponent(null);
                intent.setSelector(null);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.setData(Uri.parse(skip));
                mContext.startActivity(intent);
                result.success("1");
            } catch (Exception e) {
                result.success("0");
            }
        } else {
            result.success("0");
        }
    }

    /**
     *
     */
    public boolean installApk(String path) {
        if (path == null) {
            io.flutter.Log.e("RcPlugin", "文件路径不存在");
            return false;
        }
        try {
            File file = new File(path);
            if (file.exists()) {
                ApkUtil.installApk(mContext, file);
                return true;
            } else {
                io.flutter.Log.e("RcPlugin", "apk文件不存在");
                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
            Log.e("RcPlugin", e.getMessage() + "");
        }
        return false;
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        onAttachedToActivity(binding.getActivity());
        binding.addActivityResultListener(getActivityResultListener());
    }

    private void onAttachedToActivity(Activity activity) {
        this.mActivity = activity;
    }

    /**
     * 创建 ActivityResult 监听
     *
     * @return ActivityResult 监听
     */
    private PluginRegistry.ActivityResultListener getActivityResultListener() {
        return this;
    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {

    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

    }

    @Override
    public void onDetachedFromActivity() {

    }

    @Override
    public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
//        if (requestCode == 10086 && resultCode == Activity.RESULT_OK) {
//            return true;
//        }
        return false;
    }
}


